package edu.gduf.parcel.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;

import edu.gduf.parcel.common.constant.ParcelConstant;
import edu.gduf.parcel.common.vo.Result;
import edu.gduf.parcel.service.AdminService;
import edu.gduf.parcel.vo.AdminVo;

@Controller
@RequestMapping("/admin")
public class AdminController {
	@Autowired
	private AdminService adminService;

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView getLoginPage(Result result) {
		return new ModelAndView("admin/login", "result", result);
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView doLogin(@RequestBody JSONObject json) {
		AdminVo admin = json.toJavaObject(AdminVo.class);
		ModelAndView mvn = new ModelAndView();
		Result result = adminService.doLogin(admin);
		System.out.println(result);
		if (ParcelConstant.Result.CODE_SUCESS.equals(result.getCode()))
			mvn.setViewName("admin/main");
		else
			mvn.setViewName("redirect:admin/login");
		mvn.addObject("result", result);
		return mvn;
	}

}
